﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace NUnit.SharePointContrib
{
    public abstract class SharePoint
    {
      
        public static void SPWebAccessor(Action<SPWeb> action)
        {
           
            SPSite sitecollection = null;

            if (TryFindSiteCollection(out sitecollection) == false)
            {
                throw new Exception("SharePointContrib not found. Run the powershell script Setup.ps1 located in the PowerShell folder.'");
            }


            try
            {
                action(sitecollection.RootWeb);
            }
            finally
            {
                sitecollection.Dispose();
            }

        }

        public static bool TryFindSiteCollection(out SPSite sitecollection)
        {
            try
            {
                var url = Environment.GetEnvironmentVariable("SharePointContribUrl", EnvironmentVariableTarget.Machine);
                sitecollection = new SPSite(url);

                return sitecollection != null;
            }
            catch
            {
                sitecollection = null;
                return false;
            }
        }


    }
}
